﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SceneBuilder : MonoBehaviour {

    private float offset = 0;

    public static GameObject wallCube = null;

    private static SceneBuilder _instance = null;
    public static SceneBuilder Instance
    {
        get
        {
            lock (typeof(SceneBuilder))
            {
                if (_instance == null)
                    _instance = new SceneBuilder();
            }
            return _instance;
        }
    }

    public void BuildScene()
    {
        if (wallCube == null)
        {
            wallCube = GameObject.Find("wallCube");
            if (wallCube == null)
            {
                wallCube = GameObject.Find("WallCube");
            }
        }
        //BuildBorder();

        List<Room> rooms = DataStorage.Instance.GetRoomList();

        int i=0;

        //foreach (Room roomElement in rooms) //TODO: SceneBuilder muss Anfang und Ende von Wänden anpassen
        //{
        //    if (!roomElement.used)
        //    {
        //        GameObject parent = new GameObject(i.ToString());
        //        //Top Wall
        //        GameObject wall = GameObject.CreatePrimitive(PrimitiveType.Cube);
        //        wall.transform.position = new Vector2(roomElement.GetXLeft() + (float)(roomElement.GetWidth() - 1) / 2, roomElement.GetYTop() + offset);
        //        wall.transform.localScale = new Vector3(roomElement.GetWidth(), 1, 1);
        //        wall.transform.parent = parent.transform;
        //        wall.name = "room";
        //        //Bottom Wall
        //        wall = GameObject.CreatePrimitive(PrimitiveType.Cube);
        //        wall.transform.position = new Vector2(roomElement.GetXLeft() + (float)(roomElement.GetWidth() - 1) / 2, roomElement.GetYBottom() - offset);
        //        wall.transform.localScale = new Vector3(roomElement.GetWidth(), 1, 1);
        //        wall.transform.parent = parent.transform;

        //        wall.name = "room";
        //        //Left Wall
        //        wall = GameObject.CreatePrimitive(PrimitiveType.Cube);
        //        wall.transform.position = new Vector2(roomElement.GetXLeft() - offset, roomElement.GetYTop() - (float)(roomElement.GetHeight() - 1) / 2);
        //        wall.transform.localScale = new Vector3(1, roomElement.GetHeight(), 1);
        //        wall.transform.parent = parent.transform;

        //        wall.name = "room";
        //        //Right Wall
        //        wall = GameObject.CreatePrimitive(PrimitiveType.Cube);
        //        wall.transform.position = new Vector2(roomElement.GetXRight() + offset, roomElement.GetYTop() - (float)(roomElement.GetHeight() - 1) / 2);
        //        wall.transform.localScale = new Vector3(1, roomElement.GetHeight(), 1);
        //        wall.transform.parent = parent.transform;

        //        wall.name = "room";
        //    }
        //    i++;
        //}

            string[,] tiles = MapOverview.Instance.GetTileMap();
            //if (!roomElement.used)
            //{
            for (int y = tiles.GetLength(1) - 1; y >= 0; y--)
            {
                string temp = "";
                for (int x = 0; x < tiles.GetLength(0); x++)
                {
                    if (tiles[x, y].Equals("w"))
                    {
                        GameObject wall = Instantiate(wallCube, new Vector2(x, y), Quaternion.identity) as GameObject;

                        wall.name = "room";
                    }
                }
            }
    }

    private void BuildBorder()
    {
        //Top Wall
        GameObject wall = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), new Vector2(DataStorage.Instance.GetMapWidth()/2, DataStorage.Instance.GetMapHeight()), Quaternion.identity) as GameObject;
        wall.transform.localScale = new Vector3(DataStorage.Instance.GetMapWidth() + 2, 1, 1);
        wall.name = "border";
        //Bottom Wall
        wall = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), new Vector2(DataStorage.Instance.GetMapWidth() / 2, -1), Quaternion.identity) as GameObject;
        wall.transform.localScale = new Vector3(DataStorage.Instance.GetMapWidth()+2, 1, 1);
        wall.name = "border";

        //Left Wall
        wall = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), new Vector2(-1, DataStorage.Instance.GetMapHeight()/2), Quaternion.identity) as GameObject;
        wall.transform.localScale = new Vector3(1, DataStorage.Instance.GetMapHeight() + 2, 1);
        wall.name = "border";

        //Right Wall
        wall = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), new Vector2(DataStorage.Instance.GetMapWidth(), DataStorage.Instance.GetMapHeight() / 2), Quaternion.identity) as GameObject;
        wall.transform.localScale = new Vector3(1, DataStorage.Instance.GetMapHeight() + 2, 1);
        wall.name = "border";

    }
}
